package com.ftg.midamall.config;

import cn.hutool.core.util.StrUtil;
import com.ftg.midamall.comm.tool.TokenUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * token处理
 *
 * @author kangjx
 * @version 1.0
 * @date 2023/11/11 上午10:22
 */
@Component
@Slf4j
public class TokenAuthFilter implements GatewayFilter{

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        HttpHeaders headers = exchange.getRequest().getHeaders();
        String authorization = headers.get("token").toString();
        if (!StrUtil.isEmpty(authorization)) {
            try {
                TokenUtil.parseTokenWithObject(authorization);
            }catch (Exception e){
                log.error(e.getMessage(),"Token验证失败");
//                throw new MallException("Token验证失败");
            }
            return chain.filter(exchange);
        }else {
            return exchange.getResponse().setComplete();
        }
    }

}